﻿using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Management.Automation;
using System.Management.Automation.Runspaces;
using System.Text;
using System.Threading.Tasks;

namespace Get_NAV
{
    public sealed class DevelopmentShell : IDisposable
    {
        public PSCommand Commands { get; set; }

        public Modeltools Modeltools { get; private set; }

        public Apptools Apptools { get; private set; }

        public Appmanagement Appmanagement { get; private set; }

        public PowerShell PowerShell { get; private set; }

        public string Path { get; private set; }

        public DevelopmentShell(string path)
        {
            Path = path;
            PowerShell = PowerShell.Create();

            Initialize();
        }
        public DevelopmentShell(string path, PowerShell powerShell)
        {
            Path = path;
            PowerShell = powerShell;

            Initialize();
        }

        #region Generaltools

        public void GetCommand()
        {
            Command getCommand = new Command("Get-Command");
            getCommand.Parameters.Add(
                "Module",
                "Microsoft.Dynamics.Nav.Model.Tools, Microsoft.Dynamics.Nav.Apps.Tools, Microsoft.Dynamics.Nav.Apps.Management");

            Commands.AddCommand(getCommand);
        }

        #endregion Generaltools
        
        #region Mainfunctions

        public Collection<PSObject> Invoke()
        {
            Collection<PSObject> collection = new Collection<PSObject>();

            SetCommands();
            foreach (var item in PowerShell.Invoke())
            {
                collection.Add(item);
            }

            SetModelCommands();
            foreach (var item in PowerShell.Invoke())
            {
                collection.Add(item);
            }

            SetAppCommands();
            foreach (var item in PowerShell.Invoke())
            {
                collection.Add(item);
            }

            SetManagementCommands();
            foreach (var item in PowerShell.Invoke())
            {
                collection.Add(item);
            }


            ClearCommands();

            return collection;
        }

        public void Dispose()
        {
            Commands.Clear();
            Commands = null;

            PowerShell.Dispose();
            PowerShell = null;

            Path = null;
        }

        #endregion Mainfunctions

        #region Privatefunctions

        private void Initialize()
        {
            Command import = new Command("Import-Module");
            import.Parameters.Add("Name", Path);

            PowerShell.Commands.AddCommand(import);
            PowerShell.Invoke();
        }
        
        private void SetCommands()
        {
          PowerShell.Commands = Commands;
        }

        private void SetModelCommands()
        {
            Commands = Modeltools.Commands;
        }

        private void SetAppCommands()
        {
            Commands = Apptools.Commands;
        }

        private void SetManagementCommands()
        {
            Commands = Appmanagement.Commands;
        }

        private void ClearCommands()
        {
            Commands.Clear();
            Modeltools.Commands.Clear();
            Apptools.Commands.Clear();
            Appmanagement.Commands.Clear();
            PowerShell.Commands.Clear();
        }

        #endregion Privatefunctions

    }
}
